package com.dw.controller;

import com.alibaba.fastjson.JSON;
import com.dw.entity.Question;
import com.dw.entity.User;
import com.dw.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description
 * @Author dw
 * @Date 2020/1/9 3:18
 * @Version 1.0
 **/
@Controller
@RequestMapping("/answer")
public class AnswerController {

    /**
     * 返回到前端的数据集合
     **/
    Map<String,Object> resultMap;

    /**
     * 数据列名集合
     **/
    List<String> columnList;


    /**
     * 实体属性集合
     **/
    List<String> propertyList;

    /**
     * 条件集合
     **/
    Map<String,String> paramMap;

    /**
     * 用户服务层接口
     **/
    @Autowired
    private UserServiceImpl userService;

    /**
     问题服务
     */
    @Autowired
    private QuestionServiceImpl questionService;

    /**
     问题类型服务
     */
    @Autowired
    private QueTypeServiceImpl queTypeService;

    /**
     选项服务
     */
    @Autowired
    private ChoiceServiceImpl choiceService;

    /**
     答案服务
     */
    @Autowired
    private AnswerServiceImpl answerService;

    @RequestMapping("/addAnswer")
    @ResponseBody
    public Object addAnswer(@RequestParam Map<String,String> requestParamMap, String voteBy, HttpSession session){
        System.out.println(requestParamMap);
        //查询所有的问题
        paramMap = new HashMap<>();
        resultMap = new HashMap<>();
        paramMap.put("column","voteBy");
        paramMap.put("property",voteBy);
        List<Question> questionList = questionService.findAll("question",paramMap);
        for (Question q : questionList) {
            //审批题型
            if("4".equals(q.getQueType().toString())){
                //执行插入问题答案
                columnList = new ArrayList<>();
                propertyList = new ArrayList<>();
                //插入问题
                columnList.add("`answer`");
                columnList.add("createBy");
                columnList.add("answerBy");
                columnList.add("createTime");
                columnList.add("answerTime");
                columnList.add("queId");
                columnList.add("`check`");
                columnList.add("voteBy");

                if(requestParamMap.get(q.getQueId().toString()) != null){
                    propertyList.add("同意");

                }else{
                    propertyList.add("不同意");
                }
                propertyList.add("2019122401");
                propertyList.add(((User)session.getAttribute("user")).getUserId());
                propertyList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
                propertyList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
                propertyList.add(q.getQueId().toString());
                propertyList.add("0");
                propertyList.add(voteBy);

                questionService.insert("answer",columnList,propertyList);
            }
            if("2".equals(q.getQueType().toString())){
                //多选
                    //执行插入问题答案
                    columnList = new ArrayList<>();
                    propertyList = new ArrayList<>();
                    //插入问题
                    columnList.add("`answer`");
                    columnList.add("createBy");
                    columnList.add("answerBy");
                    columnList.add("createTime");
                    columnList.add("answerTime");
                    columnList.add("queId");
                    columnList.add("`check`");
                    columnList.add("voteBy");

                    StringBuffer stringBuffer = new StringBuffer();
                    for (int i = 0; i < 10 ; i++) {
                        if(requestParamMap.get(q.getQueId().toString()+"."+i) != null){
                            stringBuffer.append("  "+requestParamMap.get(q.getQueId().toString()+"."+i));

                        }else{

                        }
                    }
                    propertyList.add(stringBuffer.toString());
                    propertyList.add("2019122401");
                    propertyList.add(((User)session.getAttribute("user")).getUserId());
                    propertyList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
                    propertyList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
                    propertyList.add(q.getQueId().toString());
                    propertyList.add("0");
                    propertyList.add(voteBy);

                    questionService.insert("answer",columnList,propertyList);
                }

                if("1".equals(requestParamMap.get(q.getQueType().toString()))
                        || "3".equals(requestParamMap.get(q.getQueType().toString())) ||
                        "5".equals(requestParamMap.get(q.getQueType().toString()))){
                    //其他题型
                    //执行插入问题答案
                    columnList = new ArrayList<>();
                    propertyList = new ArrayList<>();
                    //插入问题
                    columnList.add("`answer`");
                    columnList.add("createBy");
                    columnList.add("answerBy");
                    columnList.add("createTime");
                    columnList.add("answerTime");
                    columnList.add("queId");
                    columnList.add("`check`");
                    columnList.add("voteBy");

                    propertyList.add(requestParamMap.get(q.getQueId().toString()));
                    propertyList.add("2019122401");
                    propertyList.add(((User)session.getAttribute("user")).getUserId());
                    propertyList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
                    propertyList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
                    propertyList.add(q.getQueId().toString());
                    propertyList.add("0");
                    propertyList.add(voteBy);

                    questionService.insert("answer",columnList,propertyList);

            }


        }
        resultMap.put("msg","ok");

        return JSON.toJSON(resultMap);
    }


    /**
     * 跳转到审批页面
     *
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/goUpdateCheck")
    public String goUpdateCheck(){
        return "answer_list_sp";
    }

    /**
     * 主管审批
     *
     * @param ansId
     * @param check
     * @return java.lang.Object
     */
    @RequestMapping("/updateCheck")
    @ResponseBody
    public Object updateCheck(String check,String ansId){
        columnList = new ArrayList<>();
        propertyList = new ArrayList<>();
        Map<String,String> idMap = new HashMap<>();
        resultMap = new HashMap<>();
        columnList.add("`check`");

        propertyList.add(check);

        idMap.put("column","ansId");
        idMap.put("property",ansId);

        if(answerService.update("answer",columnList,propertyList,idMap) > 0){
            resultMap.put("msg","ok");
        }
        else{
            resultMap.put("msg","error");
        }
        return JSON.toJSON(resultMap);
    }
}
